/*--------------------------------------------------------------------------*/
/* Project:			Mouse Control Main Header Source				   	    */
/* File:        	Mouse.h                                                 */
/* Edited by:      	Suneung, Park                                           */
/*                 	yarmini@hanmail.net                                     */
/*                                                                          */
/* Date:           	2005/06/03                      	                    */
/*--------------------------------------------------------------------------*/
/* Purpose:        	Definition of Mouse Pin Address, Function Prototype, 	*/
/*			   	 	,Base constant											*/
/*--------------------------------------------------------------------------*/

#ifndef	__MOUSE_H
#define	__MOUSE_H






/*------------------------------------------------------*/
/*	Declation of Mouse Data, Clock Pin Address  	 	*/
/*------------------------------------------------------*/

// Data, Clock Pin Read/Write Address
#define	MOUSE_WR_DT_ADDR		0x0020C3F8
#define	MOUSE_WR_CLK_ADDR		0x0020E3F8

#define	MOUSE_RD_DT_ADDR		0x000123F8
#define	MOUSE_RD_CLK_ADDR		0x000143F8





/*------------------------------------------------------*/
/*	Declation of Delay Timing(microSecond Unit)	 		*/
/*------------------------------------------------------*/
/*#define	I2C_T_BUF				DELAY_1US		//DELAY_288NS
#define	I2C_T_HD_STA			DELAY_1US		//DELAY_576NS	 DELAY_72NS
#define	I2C_T_LOW				DELAY_1US		//DELAY_288NS
#define	I2C_T_HIGH				DELAY_1US		//DELAY_576NS	 DELAY_72NS
#define	I2C_T_SU_DAT			DELAY_144NS
#define	I2C_T_SU_STO			DELAY_1US		//DELAY_576NS	 DELAY_72NS


#define	I2C_WAIT_TIME_LIMIT		250	// microSecond unit
*/



/*------------------------------------------------------*/
/*	Declation of Pin Control Funtion			 		*/
/*------------------------------------------------------*/

//#define	MOUSE_WR_DT( fX )		({ outpw( MOUSE_WR_DT_ADDR, fX ); /*DELAY_1US*/ })
//#define	MOUSE_WR_CLK( fX )		({ outpw( MOUSE_WR_CLK_ADDR, fX ); /*DELAY_1US*/ })

//#define	MOUSE_RD_DT				inp( MOUSE_RD_DT_ADDR )
//#define	MOUSE_RD_CLK			inp( MOUSE_RD_CLK_ADDR )

#define	DATA_LINE_OUTPUT		{EALLOW; GpioCtrlRegs.GPADIR.bit.GPIO4 = 1; EDIS;\
								GpioDataRegs.GPASET.bit.GPIO2= 1;					}

#define	DATA_LINE_INPUT			{EALLOW; GpioCtrlRegs.GPADIR.bit.GPIO4 = 0; EDIS; \
								GpioDataRegs.GPACLEAR.bit.GPIO2 = 1;					}

#define	CLOCK_LINE_OUTPUT		{EALLOW; GpioCtrlRegs.GPADIR.bit.GPIO5 = 1; EDIS;\
								GpioDataRegs.GPASET.bit.GPIO3 = 1;					}

#define	CLOCK_LINE_INPUT		{EALLOW; GpioCtrlRegs.GPADIR.bit.GPIO5 = 0; EDIS;\
								GpioDataRegs.GPACLEAR.bit.GPIO3 = 1;					}

//#define	MOUSE_WR_DT( fx )		{GpioDataRegs.GPADAT.bit.GPIO4 = fx;}

								// 0 : input 1 : output								
#define	MOUSE_WR_CLK_L			{GpioDataRegs.GPACLEAR.bit.GPIO5 = 1;}

								// 0 : input 1 : output
#define	MOUSE_WR_CLK_H			{GpioDataRegs.GPASET.bit.GPIO5 = 1;}


#define	MOUSE_RD_DT				GpioDataRegs.GPADAT.bit.GPIO4
#define	MOUSE_RD_CLK			GpioDataRegs.GPADAT.bit.GPIO5

#define CLOCK_DELAY				DELAY_US(35)
#define	END_CMD_DELAY			DELAY_US(200)


/*------------------------------------------------------*/
/*	Declation of Command, Value					 		*/
/*------------------------------------------------------*/
// Command Set
#define	MOUSE_RESET_CMD			0XFF
#define	MOUSE_RESEND_CMD		0XFE
#define	MOUSE_ERROR_CMD			0XEC
#define	MOUSE_SET_DEFAUL		0XF6
#define	MOUSE_DIS_DATA_REPORT	0XF5
#define	MOUSE_EN_DATA_REPORT	0XF4
#define	MOUSE_SET_SAMPLE_RATE	0XF3
#define	MOUSE_GET_DEV_ID		0XF2
#define	MOUSE_SET_REMOTE_MODE	0XF0
#define	MOUSE_SET_WRAP_MODE		0XEE
#define	MOUSE_RESET_WRAP_MODE	0XEC
#define	MOUSE_READ_DATA			0XEB
#define	MOUSE_SET_STEARM_MODE	0XEA
#define	MOUSE_STATUS_REQUEST	0XE9
#define	MOUSE_SET_RESOLUTION	0XE8
#define	MOUSE_SET_SCALE2_1		0XE7
#define	MOUSE_SET_SCALE1_1		0XE6

//PC Send Message
#define	MOUSE_ACK				0xFA
#define	MOUSE_SELF_TEST			0XAA
#define	MOUSE_ID				0X00


#define	MOUSE_POS_X_MAX			360
#define	MOUSE_POS_Y_MAX_NTSC	240
#define	MOUSE_POS_Y_MAX_PAL		288


// Event
#define	MOUSE_EVENT_NONE		0x00
#define	MOUSE_EVENT_CLICK_L		0x01
#define	MOUSE_EVENT_CLICK_R		0x02

// Sensitivity Level
#define	MOUSE_SENLEVEL_MAX_N	4


/*------------------------------------------------------*/
/*	Definition of Mouse Transmision mode				*/
/*------------------------------------------------------*/

#define	STREAM_MODE			0
#define	REMOTE_MODE 		1
#define	RESET_MODE			2

/*------------------------------------------------------*/
/*	Declation of Structure								*/
/*------------------------------------------------------*/

// Mouse Infomation
typedef struct
{
	// Status
	Uint16 	bStatus;

	Uint16	bResolution;
	Uint16	tbReserved[2];

	// Position
	Uint16	hX;
	Uint16	hY;

	Uint16 hPreX;
	Uint16 hPreY;

	// Button Status
	Uint16	bLButton;
	Uint16	bRButton;

	// Event
	Uint16	bEvent;


	// Refresh
	Uint16	fRefresh;
}structMouse;


// Mouse Area
typedef struct
{
	// Start Position
	Uint16	hStartX;
	Uint16	hStartY;

	// Size
	Uint16	hSizeX;
	Uint16	hSizeY;
}structMouse_Area;


extern	structMouse		symMouse;

/*------------------------------------------------------*/
/*	Declation of Prototypes	      		 				*/
/*------------------------------------------------------*/

//-- Hardware Control ----------------------
interrupt void	MouseReceiveData( void );
void Mouse_DataTransmition( Uint16 SendData );
Uint16	Mouse_Read( Uint16 *pbData );
Uint16	Mouse_Write( const Uint16 bData );
Uint16	Mouse_WaitClk( const Uint16 bClk );
Uint16	Mouse_WaitData( const Uint16 bClk );


//-- Application ---------------------------
Uint16	Mouse_Init( void );
Uint16	Mouse_Reset( void );
void 	Mouse_SetSampleRate( const Uint16 samplerate );
void	Mouse_SetResolution( const Uint16 data );
void 	Mouse_SetScale( Uint16 cmd );
void 	Mouse_WaitTransState(void);
void 	Mouse_SetTransState(void);
Uint16 	Mouse_GetTransState( void);
void 	Mouse_ClearTransState(void);
void 	Mouse_PutReceiveData( Uint16 putData );
Uint16 	Mouse_GetReceiveData( void );

void 	Mouse_ExcutionCmd( Uint16 cmd );
void	Mouse_EnableDataReport( void );
void 	Mouse_DisableDataReport( void );

void 	Mouse_DataWrite( Uint16 Data );
void	Mouse_SetRequesttoSend( void );
void	Mouse_ClearRequesttoSend( void );
Uint16	Mouse_GetRequesttoSendState(void);


#endif
